package login2;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/login2")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf8");
        //获取登录信息
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        if(!"zhangsan".equals(username) || !"123".equals(password)) {
            resp.getWriter().write("很抱歉，您的账户或密码有误!");
            return;
        }
        //服务器打印日志
        System.out.println(username + " 上线");
        //从数据库获取登录次数
        int loginCount = 0;
        try {
            loginCount = load(username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //将登录数据保存到数据库
        try {
            save(username, String.valueOf(++loginCount));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //设置Session
        HttpSession session = req.getSession(true);
        session.setAttribute("username", username);
        session.setAttribute("loginCount", loginCount);
        //跳转网页到index；
        resp.sendRedirect("index2");
    }

    //数据库——取出数据
    private int load(String username) throws SQLException {
        //创建数据源
        DataSource dataSource = SingletonLazy.getDataSource();
        //建立连接
        Connection connection = dataSource.getConnection();
        //构造sql
        String sql = "select loginCount from message where name = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, username);
        //执行sql
        ResultSet resultSet = statement.executeQuery();
        int loginCount = 0;
        while(resultSet.next()) {
            loginCount = resultSet.getInt("loginCount");
        }
        //关闭数据库
        statement.close();
        connection.close();
        return loginCount;
    }
    //数据库——存储数据
    private void save(String username, String loginCount) throws SQLException {
        //创建数据源
        DataSource dataSource = SingletonLazy.getDataSource();
        //建立连接
        Connection connection = dataSource.getConnection();
        //构造sql
        String sql = "update message set loginCount = ? where name = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, loginCount);
        statement.setString(2, username);
        //执行sql
        int ret = statement.executeUpdate();
        System.out.println("修改数据库" + ret + "条数据");
        //关闭数据库
        statement.close();
        connection.close();
    }
}













